﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using NeGroid.ChampionshipManager.Core;
using NeGroid.ChampionshipManager.Persistance;
using NeGroid.ChampionshipManager.Web.Models;

namespace NeGroid.ChampionshipManager.Web.Controllers
{
    public class TeamsController : Controller
    {
		private readonly IEntityRepository<Team> _teamRepository;
		public TeamsController(IEntityRepository<Team> teamRepository)
		{
			_teamRepository = teamRepository;
		}

        public ActionResult Index()
        {
            return View(_teamRepository.Get());
        }

		public ActionResult Add()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Add(TeamModel team)
		{
			if (!ModelState.IsValid)
			{
				return View(team);
			}

			_teamRepository.Insert(Mapper.Map<Team>(team));
			_teamRepository.SaveChanges();

			return RedirectToAction("Index");
		}
	}
}
